package cz.muni.fi.pa165.sme.services.converter;

import cz.muni.fi.pa165.sme.api.enumeration.SexTO;
import cz.muni.fi.pa165.sme.api.enumeration.UserRoleTO;
import cz.muni.fi.pa165.sme.api.to.UserTO;
import cz.muni.fi.pa165.sme.dao.entity.User;
import cz.muni.fi.pa165.sme.dao.enumeration.Sex;
import cz.muni.fi.pa165.sme.dao.enumeration.UserRole;
import org.springframework.core.convert.converter.Converter;

/**
 * @author forman
 */
public class User2UserTOConverter implements Converter<User, UserTO> {

	@Override
	public UserTO convert(final User source) {
		if (source == null) {
			return null;
		}

		final UserTO target = new UserTO();

		target.setDateOfBirth(source.getDateOfBirth());
		target.setFirstName(source.getFirstName());
		target.setSecondName(source.getSecondName());
		target.setHeight(source.getHeight());
		target.setId(source.getId());
		target.setWeight(source.getWeight());
		target.setSexTO(convertSex(source.getSex()));
		target.setLogin(source.getLogin());
		target.setPassword(source.getPassword());
		target.setRoleTO(convertRole(source.getRole()));

		return target;
	}

	private SexTO convertSex(final Sex sex) {
		if (sex == null) {
			return null;
		}
		return SexTO.valueOf(sex.name());
	}

	private UserRoleTO convertRole(final UserRole userRole) {
		if(userRole == null) {
			return null;
		}
		return UserRoleTO.valueOf(userRole.name());
	}
}
